package com.daikuan.lizh.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.daikuan.lizh.R;
import com.daikuan.lizh.bmobBean.ListConfig;
import com.daikuan.lizh.utils.ImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jiming1 on 2017/7/1.
 */
public class HomeListAdapter extends BaseAdapter {
	private Context context;
	private List<ListConfig> listConfigs;

	public HomeListAdapter(Context context, List<ListConfig> listConfigs) {
		this.context = context;
		this.listConfigs = listConfigs;
	}

	@Override
	public int getCount() {
		return listConfigs.size();
	}

	@Override
	public ListConfig getItem(int i) {
		return listConfigs.get(i);
	}

	@Override
	public long getItemId(int i) {
		return 0;
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		BaseViewHolder viewHolder = null;
		final ListConfig hotChannel = getItem(i);
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_home, viewGroup, false);
			viewHolder = new BaseViewHolder(view);
			view.setTag(viewHolder);
		} else {
			viewHolder = (BaseViewHolder) view.getTag();
		}

		viewHolder.tvName.setText(hotChannel.channel_name);
		viewHolder.tvQuotaRange.setText(hotChannel.money_range);
		viewHolder.tvInfo.setText(hotChannel.channel_dis);
//        viewHolder.tvSuccessRate.setText(hotChannel.successRate);
		if (hotChannel.channel_logo == null) {
			ImageLoader.loadImage(context, "", viewHolder.ivImage);
		} else {
			ImageLoader.loadImage(context, hotChannel.channel_logo.url, viewHolder.ivImage);
		}


		return view;
	}

	public void update(List<ListConfig> beans) {
		this.listConfigs.clear();
		this.listConfigs.addAll(beans);
		notifyDataSetChanged();
	}

	public void add(List<ListConfig> beans) {
		this.listConfigs.addAll(beans);
		notifyDataSetChanged();
	}

	class BaseViewHolder {
		@Bind(R.id.ivImage)
		ImageView ivImage;
		@Bind(R.id.tvName)
		TextView tvName;
		@Bind(R.id.tvQuotaRange)
		TextView tvQuotaRange;
		@Bind(R.id.tvSuccessRate)
		TextView tvSuccessRate;
		@Bind(R.id.tvInfo)
		TextView tvInfo;

		public BaseViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}


}
